<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="SHZY-common-search-box">
      <el-row :gutter="20">
        <el-form @submit.native.prevent="">
          <el-col :span="6">
            <!-- <el-form-item label="">
        <el-input
          size="small"
          v-model="formInline.userName"
          placeholder="输入用户名"
        ></el-input>
      </el-form-item> -->
            <el-form-item label="角色编码">
              <el-input
                size="small"
                v-model="formInline.roleCode"
                placeholder="角色编码"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="角色名称">
              <el-input
                size="small"
                v-model="formInline.roleName"
                placeholder="角色名称"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button
                size="small"
                type="primary"
                icon="el-icon-search"
                @click="search()"
                >查询</el-button
              >
              <el-button
                size="small"
                icon="el-icon-refresh-right"
                @click="reset()"
                >重置</el-button
              >
            </el-form-item>
          </el-col>
        </el-form>
        <!-- <el-col :span="7">
          
          <el-input placeholder="请输入内容">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="2">
          <el-button type="primary">添加用户</el-button>
        </el-col> -->
      </el-row>
    </div>
    <!-- <div class="SHZY-common-head">
        <el-button size="small" type="primary">新增</el-button>
       
        
      </div> -->
    <el-card>
      <div class="SHZY-common-head">
        <!-- <el-row :gutter="1">
          <el-col :span="1"
            ><el-button
              size="small"
              type="primary"
              icon="el-icon-plus"
              @click="addOrUpdateHandle()"
              >新增</el-button
            ></el-col
          >
          <el-col :span="1"
            ><el-button size="small" type="text" icon="el-icon-download"
              >导入</el-button
            ></el-col
          >
          <el-col :span="1"
            ><el-button size="small" type="text" icon="el-icon-delete"
              >批量删除</el-button
            ></el-col
          >
        </el-row> -->
        <div>
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            @click="addOrUpdateHandle()"
            >新增</el-button
          >
          <el-button
            v-if="false"
            size="small"
            type="text"
            icon="el-icon-download"
            >导入</el-button
          >
          <el-button v-if="false" size="small" type="text" icon="el-icon-delete"
            >批量删除</el-button
          >
        </div>
        <!-- <el-button size="small" type="primary">新增</el-button>
        <el-button size="small" type="primary">新增</el-button>
        <el-button size="small" type="primary">新增</el-button> -->
      </div>
      <div class="table-box" ref="tableBox">
        <el-table
          :data="rolelist"
          @selection-change="selectchange"
          :header-cell-style="headercellstyle"
          max-height="tableHeight +'px'"
          v-if="tableHeight"
          @sort-change="sortChange"
        >
          <el-table-column type="selection" width="55"> </el-table-column>
          <el-table-column v-if="false" prop="id" label="id"></el-table-column>
          <el-table-column
            prop="roleCode"
            label="角色编码"
            align="left"
            min-width="200px"
            sortable
          ></el-table-column>
          <el-table-column
            prop="roleName"
            label="角色名称"
            min-width="200px"
            sortable
          ></el-table-column>
          <el-table-column
            prop="status"
            label="状态"
            min-width="200px"
            sortable
          >
            <template slot-scope="scope">
              <el-tag v-if="scope.row.status == '1'" type="success"
                >正常</el-tag
              >
              <el-tag v-if="scope.row.status == '0'" type="danger">禁用</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="150px" fixed="right">
            <template slot-scope="scope">
              <el-button
                type="text"
                size="mini"
                @click="addOrUpdateHandle(scope.row.id)"
                >编辑</el-button
              >
              <el-button
                size="mini"
                type="text"
                style="color: red"
                @click="handleDelete(scope.$index, scope.row)"
                >删除</el-button
              >
              <el-dropdown placement="bottom" trigger="click">
                <el-button type="text" size="mini" class="eliconright">
                  更多<i class="el-icon-arrow-down el-icon--right"></i>
                </el-button>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item
                    @click.native="RoleUser(scope.$index, scope.row)"
                    >角色成员</el-dropdown-item
                  >
                  <el-dropdown-item
                    @click.native="Permission(scope.$index, scope.row)"
                    >角色权限</el-dropdown-item
                  >
                </el-dropdown-menu>
              </el-dropdown>
              <!-- <el-dropdown @command="handleCommand">
                <el-button type="primary" size="mini" class="eliconright">
                  更多<i class="el-icon-arrow-down el-icon--right"></i>
                </el-button>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="role-user"
                    >角色成员</el-dropdown-item
                  >
                  <el-dropdown-item command="permission"
                    >角色权限</el-dropdown-item
                  >
                </el-dropdown-menu>
              </el-dropdown> -->
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="el-pagination">
        <el-pagination
          :page-sizes="[10, 20, 30, 40]"
          :page-size="100"
          :total="total"
        >
        </el-pagination>
      </div>

      <SHZY-Form v-if="formVisible" ref="SHZYForm" @refresh="refresh" />
      <!-- 用户角色弹出层 -->
      <RoleUser v-if="roleuserdialog" ref="RoleUser" />
      <!-- 角色权限页面 -->
      <Permission v-if="permissiondialog" ref="Permission"></Permission>
    </el-card>
  </div>
</template>
<script>
import { getRoleList } from "/src/api/role.js";
import SHZYForm from "./Form";
import RoleUser from "./roleuserdialog.vue";
import Permission from "./permission.vue"
export default {
  components: { SHZYForm, RoleUser,Permission },
  data() {
    return {
      roleuserdialog: false,
      tableHeight: 0,
      total: 0,
      formVisible: false,
      permissiondialog:false,
      //查询参数
      formInline: {
        // page: 1,
        // limit: 10,
        // deptId: '',
        roleCode: "",
        roleName: "",
        // status: "",
        // currentpage: 1,
        // pagesize: 10,
        // sort: "desc",
        // sidx: "",
        // userMobile: '',
        // isLock: ''
      },
      //分页排序参数
      listquery: {
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "",
      },
      rolelist: [],
      selectdata: [],
    };
  },
  created() {
    this.getrolelist();
  },
  methods: {
    //查询方法
    search() {
      // console.log("search");
      this.listquery = {
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "no",
      };
      this.getrolelist();
      // userList(this.formInline).then((res) => {
      //   this.userlist = res.list;
      // });
    },
    sortChange({ column, prop, order }) {
      // console.log(order);
      // console.log(prop);
      this.listquery.sort = order == "ascending" ? "asc" : "desc";
      this.listquery.sidx = !order ? "" : prop;
      this.getrolelist();
    },
    //重置
    reset() {
      this.formInline.roleCode = undefined;
      this.formInline.roleName = undefined;
      this.search();
    },
    refresh(isrRefresh) {
      this.formVisible = false;
      if (isrRefresh) this.reset();
    },
    // reset() {
    //   console.log("调用刷新的方法");
    //   // for (let key in this.query) {
    //   //     this.query[key] = undefined
    //   // }
    //   // this.listQuery = {
    //   //     currentPage: 1,
    //   //     pageSize: 20,
    //   //     sort: "desc",
    //   //     sidx: "",
    //   // }
    //   // this.initData()
    // },
    getrolelist() {
      // console.log("role");
      let _query = {
        ...this.listquery,
        ...this.formInline,
        //menuId: this.menuId,
      };
      let query = {};
      for (let key in _query) {
        if (Array.isArray(_query[key])) {
          query[key] = _query[key].join();
        } else {
          query[key] = _query[key];
        }
      }
      // console.log(query);
      getRoleList(query).then((res) => {
        // console.log("用户列表输出");
        // console.log(res);
        this.total = res.count;
        this.rolelist = res.list;
      });
    },
    selectchange(val) {
      this.selectdata = val;
      // console.log(this.selectdata);
    },
    handleEdit(index, row) {
      console.log(index, row);
    },
    handleDelete(index, row) {
      console.log(index, row);
    },
    headercellstyle({ row, rowIndex }) {
      // console.log(rowIndex);
      return "background:#f5f7fa";
    },
    addOrUpdateHandle(id) {
      this.formVisible = true;
      this.$nextTick(() => {
        this.$refs.SHZYForm.init(id);
      });
    },
    handleCommand(command) {
      this.$message("click on item " + command);
    },
    RoleUser(index, row) {
      // console.log("角色成员", index, row.id);
      this.roleuserdialog=true;
      this.$nextTick(() => {
        this.$refs.RoleUser.init(row.id);
      });
    },
    Permission(index, row) {
      console.log("角色权限", index, row);
      this.permissiondialog=true;
      this.$nextTick(()=>{
        this.$refs.Permission.init(row.id);
      })
    },
  },
  mounted() {
    this.tableHeight = this.$refs.tableBox.clientHeight - 40;
  },
};
</script>
<style lang="less" scoped>
.el-pagination {
  background: #fff;
  text-align: right;
}
.table-box {
  height: calc(100vh - 330px); /*示例中顶部区域固定高度190px*/
}
.eliconright {
  margin-left: 10px;
}
// ::v-deep .el-table--border th.el-table__cell,
// ::v-deep .el-table td.el-table__cell {
//   border-bottom: none !important;
// }

// ::v-deep .el-table--border .el-table__cell {
//   border-right:none !important;
// }

// ::v-deep .el-table--group, .el-table--border{
//   border: none !important;
// }
// .el-table__cell{
//   padding: 0px 0;
// }
</style>>